package com.qf.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.UUID;

/**
 * @version v1.0.0
 * @Description:
 * @author: 赵英杰
 * @date: 2022.02.14
 * @since 1.0
 */
@Controller
public class UploadController {

    @RequestMapping("/upload")
    public String upload(String username, MultipartFile file, HttpSession session) {
        System.out.println("username" + username);
        //动态获取服务路径
        String realPath = session.getServletContext().getRealPath("/upload");
        System.out.println(realPath);
        if (!new File(realPath).exists()) {
            new File(realPath).mkdirs();
        }
            /*文件的原始名称*/
            String originalFilename = file.getOriginalFilename();
            String uuid = UUID.randomUUID().toString();
            System.out.println(file);
            try {
                file.transferTo(new File(realPath + "/" + uuid + originalFilename));
            } catch (IOException e) {

            }
            return "hello";
        }
    }

